﻿Imports System.Data

Partial Class AMLO_Amlo1_01_Popup
    Inherits Web.UI.BasePage

    Dim dsAmlo As DataSet
    Dim clsDatabase As clsDatabase
    Dim clsSessionMenuRight As clsSessionMenuRight

    Dim strCbsSeq As String
    Dim strCbsTranDate As String
    Dim strSolId As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            chkIsLogin()
            CreateControlMP()
            'btnSave.Attributes.Add("onclick", "NoDoubleSubmit();")
            btnSave.Attributes.Add("onclick", "return confirm('ต้องการบันทึกข้อมูล');")

            clsSessionMenuRight = Session("ClsMenuRight")
            dsAmlo = New DataSet()

            'hdfCbsSeq.Value = Request.Item("CBS_SEQ")
            'hdfTranDate.Value = Request.Item("CBS_TRAN_DATE")
            'hdfSolId.Value = Request.Item("SOL_ID")

            hdfCbsSeq.Value = Request.QueryString("CBS_SEQ")
            hdfTranDate.Value = Request.QueryString("CBS_TRAN_DATE")
            hdfSolId.Value = Request.QueryString("SOL_ID")

            strCbsSeq = hdfCbsSeq.Value
            strCbsTranDate = hdfTranDate.Value
            strSolId = hdfSolId.Value

            If Not (String.IsNullOrEmpty(strCbsSeq)) Then
                InitData()
            End If
        Else
            dsAmlo = Session("ds")

            strCbsSeq = hdfCbsSeq.Value
            strCbsTranDate = hdfTranDate.Value
            strSolId = hdfSolId.Value
        End If
    End Sub

#Region "Function"

    Public Sub CreateControlMP() 'อ้าง control ใน masterPage
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/amlo01_01.jpg"

        Dim lbl As Label = CType(Master.FindControl("lblPage"), Label)
        lbl.Text = "เลือกรายการที่สาขาบันทึก"
    End Sub

    Private Sub IsDataBind()

        Dim strSql As String

        If dsAmlo.Tables.Contains("AmloPopup") Then
            dsAmlo.Tables.Clear()
        End If

        strSql = "select * " _
                & " from v_amlo101_CBS t" _
                & " where(t.cbs_seq = " & strCbsSeq _
                & " and t.cbs_full_rptno is not null)" _
                & " or (t.cbs_seq is null "

        If Not (String.IsNullOrEmpty(strCbsTranDate)) Then
            strSql = strSql & " and t.transdate = to_date('" & strCbsTranDate & "', 'yyyymmdd')"
        End If

        strSql = strSql & " and t.sol_id = '" & strSolId & "'"
        strSql = strSql & ")"

        clsDatabase = New clsDatabase()
        Dim dt = clsDatabase.QueryDataTable(strSql)
        dt.TableName = "AmloPopup"
        dsAmlo.Tables.Add(dt.Copy)

        If (dt.Rows.Count = 0) Then
            lblBranch.Visible = False
            lblTotal.Text = "จำนวนรายการทั้งหมด " + dt.Rows.Count.ToString + " รายการ "
        Else
            lblBranch.Visible = True
            lblTotal.Text = "จำนวนรายการทั้งหมด " + dt.Rows.Count.ToString + " รายการ "
        End If

        grdAmlo.DataSource = dsAmlo.Tables("AmloPopup")
        grdAmlo.DataBind()
    End Sub

    Private Sub InitData()
        Dim strSql As String

        strSql = "select cbs.rptseq, cbs.report_flg, cbs.full_rptno, cbs.remark, " _
                & " b.branchcode, t.amlo_tran_desc, cbs.acct_name, cbs.foracid, cbs.tran_date, cbs.tran_amt " _
                & " from amlo101_cbs cbs" _
                & " left join amlo_transaction_dim t " _
                & " on t.amlo_tran_key = cbs.tran_type" _
                & " left join sbranch b " _
                & " on b.cbs_sol_id = cbs.sol_id" _
                & " where cbs.seq = " & strCbsSeq

        clsDatabase = New clsDatabase()
        Dim dt = clsDatabase.QueryDataTable(strSql)

        If (dt.Rows.Count > 0) Then
            hdfRptSeq.Value = dt.Rows(0)("rptseq").ToString
            rdoReportStatus.SelectedValue = dt.Rows(0)("report_flg").ToString
            txtFullRptno.Text = dt.Rows(0)("full_rptno").ToString
            txtReason.Text = dt.Rows(0)("remark").ToString

            If (rdoReportStatus.SelectedValue = "Y") Then
                IsDataBind()
                grdAmlo.SelectedIndex = 0
            ElseIf (rdoReportStatus.SelectedValue = "N") Then
                grdAmlo.Columns(0).Visible = False
            End If
        Else
            hdfRptSeq.Value = ""
            rdoReportStatus.SelectedIndex = -1
            txtFullRptno.Text = ""
            txtReason.Text = ""
        End If

        grdAmloCbs.DataSource = dt
        grdAmloCbs.DataBind()

    End Sub
#End Region

#Region "Event Gridview"
    Protected Sub grdAmlo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdAmlo.SelectedIndexChanged
        Dim strRptSeq As String = CType(grdAmlo.Rows(grdAmlo.SelectedIndex).FindControl("hdfRptSeq"), HiddenField).Value
        Dim strFullRptNo As String = CType(grdAmlo.Rows(grdAmlo.SelectedIndex).FindControl("hdfRptFullNo"), HiddenField).Value
        txtFullRptno.Text = strFullRptNo
        hdfRptSeq.Value = strRptSeq
    End Sub
#End Region

#Region "Event Button"
    Private Function ValidateSave(ByRef message As String) As Boolean
        Dim valid = False

        If (rdoReportStatus.SelectedIndex = -1) Then
            message = "กรุณาระบุ สถานะรายงาน\n"
        ElseIf (rdoReportStatus.SelectedValue = "Y" AndAlso txtFullRptno.Text.Trim = "") Then
            message = message & "กรุณาเลือกรายการธุรกรรม\n"
        End If

        If (txtReason.Text.Trim = "") Then
            message = message & "กรุณาระบุ เหตุผล"
        End If

        If (String.IsNullOrEmpty(message)) Then
            valid = True
        Else
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                    UpdatePanel1.GetType(), _
                                                    "clientScript", _
                                                    "alert('" & message & "');", _
                                                    True)
        End If

        Return valid
    End Function

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        clsSessionMenuRight = Session("clsMenuRight")
        Dim message As String = ""
        Try
            If (ValidateSave(message)) Then
                
                Dim strSql = "update amlo101_cbs cbs" _
                             & " set cbs.report_flg = '" & rdoReportStatus.SelectedValue & "'" _
                             & " , cbs.mapby = '" & clsSessionMenuRight.getUserName & "'" _
                             & " , cbs.mapdate = trunc(sysdate)" _
                             & " , cbs.rptseq = '" & hdfRptSeq.Value & "'" _
                             & " , cbs.full_rptno = '" & txtFullRptno.Text & "'" _
                             & " , cbs.remark = '" & txtReason.Text.Replace("'", "''") & "'" _
                             & " where cbs.seq = " & strCbsSeq

                clsDatabase = New clsDatabase
                Dim success = clsDatabase.QueryExecuteNonQuery(strSql)

                If (success) Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                        UpdatePanel1.GetType(), _
                                                        "clientScript", _
                                                        "this.close();", _
                                                        True)
                Else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                        UpdatePanel1.GetType(), _
                                                        "clientScript", _
                                                        "alert('บันทึกไม่สำเร็จ');", _
                                                        True)
                End If
            Else
                ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                    UpdatePanel1.GetType(), _
                                                    "clientScript", _
                                                    "alert('" + message + "');", _
                                                    True)

            End If
        Catch ex As Exception
            Dim strError = "Error Message :" & If(ex.InnerException Is Nothing, ex.Message, ex.InnerException.Message)
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                UpdatePanel1.GetType(), _
                                                "clientScript", _
                                                "alert('" + strError.Replace("'", "") + "')", _
                                                True)
        End Try
    End Sub
#End Region

    Protected Sub rdoReportStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoReportStatus.SelectedIndexChanged
        If rdoReportStatus.SelectedValue = "N" Then
            hdfRptSeq.Value = ""
            txtFullRptno.Text = ""
            grdAmlo.SelectedIndex = -1
            grdAmlo.Columns(0).Visible = False
        ElseIf rdoReportStatus.SelectedValue = "Y" Then
            grdAmlo.Columns(0).Visible = True
            IsDataBind()
        End If
    End Sub

    
End Class
